//
//  BookmarkWindowController.m
//  ToolbarTest
//
//  Created by Junjie Gong on 12-5-28.
//  Copyright (c) 2012年 nhn. All rights reserved.
//

#import "BookmarkWindowController.h"

@implementation BookmarkWindowController
@synthesize btnRect;
@synthesize scrollView;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc{
    [scrollView release];
    [super dealloc];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}

#define HMargin    5
#define TopMargin  55
#define BottomMargin 5
#define ItemWidth  100
#define ItemHeight 75
- (void)awakeFromNib{
    [super awakeFromNib];
    
    NSPoint p = btnRect.origin;
    NSWindow *mainw = [NSApp mainWindow];
    NSRect mainR = [mainw frame];
    NSPoint mainP = mainR.origin;
    NSRect r = self.window.frame;
    r.size.width = ItemWidth*3 + HMargin * 2;
    r.size.height = ItemHeight*3 + TopMargin + 5;
    
    mainP.x += p.x;
    mainP.y += p.y;
    mainP.x += 10;
    mainP.y -= r.size.height;

    r.origin = mainP;
    
    //[self.window setFrameOrigin:mainP];
    [self.window setFrame:r display:NO];
    
    self.scrollView = [[[NSScrollView alloc] initWithFrame:NSMakeRect(HMargin, BottomMargin, ItemWidth*3, ItemHeight*3)] autorelease];
    [scrollView setHasVerticalScroller:YES];
    [scrollView setAutohidesScrollers:YES];
    itemNumber = 9;
    
    [self updateScrollView];
    
    [self.window.contentView addSubview:scrollView];
    
}

- (void)updateScrollView{
    int moreRow = 0;
    if (itemNumber > 9) {
        moreRow = (itemNumber - 9) / 3;
        int mode = (itemNumber-9)%3;
        if (mode != 0) {
            moreRow += 1;
        }
    }
    else{
        moreRow = 0;
    }
    
    int allRow = 3 + moreRow;
    int moreWidth = 0;
    if (moreRow > 0){
        moreWidth = 15;
    }
    NSView *contentV = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 3*ItemWidth, allRow*ItemHeight)];
    
    NSRect rr = self.window.frame;
    NSSize newSize = rr.size;
    newSize.width = ItemWidth*3 + HMargin * 2 + moreWidth;
    rr.size = newSize;
    [self.window setFrame:rr display:NO];
    
    NSSize scrollNewSize = scrollView.frame.size;
    scrollNewSize.width = 3*ItemWidth + moreWidth;
    [scrollView setFrameSize:scrollNewSize];
    
    int temNumber = itemNumber;
    for (int i=allRow-1; i>=0; --i) {
        for (int j=0; j<3; j++) {
            NSImage *img = [NSImage imageNamed:@"DSC00579.JPG"];
            NSImageView *imgV = [[NSImageView alloc] initWithFrame:NSMakeRect(j*ItemWidth, i*ItemHeight, ItemWidth, ItemHeight)];
            [imgV setImage:img];
            [contentV addSubview:imgV];
            [imgV release];
            temNumber--;
            if (temNumber==0) {
                break;
            }
        }
        if (temNumber==0) {
            break;
        }
    }
    [scrollView setDocumentView:contentV];
    NSPoint currentScrollPosition=[[scrollView documentView] bounds].origin;
    currentScrollPosition.y += [[scrollView documentView] bounds].size.height;
    [[scrollView documentView] scrollPoint:currentScrollPosition];
    NSLog(@"x=%f, y=%f", currentScrollPosition.x,currentScrollPosition.y);
}

- (void)startShowWindow{
    [self showWindow:nil];
    
    eventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyDownMask handler:^(NSEvent *incomingEvent) {
        NSEvent *result = incomingEvent;
        NSWindow *targetWindowForEvent = [incomingEvent window];
        if (targetWindowForEvent != self.window) {
            [self.window close];
        } 
        else if ([incomingEvent type] == NSKeyDown) {
            if ([incomingEvent keyCode] == 53) {
                // Escape
                [self.window close];
                result = nil; // Don't process the event
            }
        }
        return result;
    }];
}

- (void)windowWillClose:(NSNotification *)notification{
    if (eventMonitor) {
        [NSEvent removeMonitor:eventMonitor];
        eventMonitor = nil;
    }
    //[self.window orderOut:nil];
    [self autorelease];
}

- (IBAction)addNumber:(id)sender{
    itemNumber += 1;
    [self updateScrollView];
}

- (IBAction)subNumber:(id)sender{
    if (itemNumber==1) {
        return;
    }
    itemNumber -= 1;
    [self updateScrollView];
}

@end
